using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Yoozoo.Mars.Got;

namespace Yoozoo.Core.Common
{
    public class ParticleQualityListener : QualityListenerMono<ParticleQualityListener.ParticleQualitySetting>
    {
        [Serializable]
        public class ParticleQualitySetting : QualitySetting
        {
            public float countFactor;
        }
        
        private HashSet<ParticleGrading> _particleGradings = new HashSet<ParticleGrading>();

        private HashSet<ParticleCountGrading> _particleCountGradings = new HashSet<ParticleCountGrading>();



        // 调试使用
        public List<ParticleGrading> ParticleGradingList;
        public List<ParticleCountGrading> ParticleCountGradingList;


        protected override void Awake()
        {
            base.Awake();
            for (int i = 0; i < 150; i++)
            {
                _particleGradings.Add(null);
            }
            _particleGradings.Clear();
            
            for (int i = 0; i < 300; i++)
            {
                _particleCountGradings.Add(null);
            }
            _particleCountGradings.Clear();
        }

        public void AddParticleGrading(ParticleGrading particleGrading)
        {
            if (particleGrading==null) return;
            _particleGradings.Add(particleGrading);
            particleGrading.ChangeLevel(GetLevel(CurrentQualitySetting));
        }

        public void RemoveParticleGrading(ParticleGrading particleGrading)
        {
            if(particleGrading==null) return;
            _particleGradings.Remove(particleGrading);
        }

        public void AddParticleCountGrading(ParticleCountGrading countGrading)
        {
            if(countGrading==null) return;
            _particleCountGradings.Add(countGrading);
            countGrading.ChangeLevel(GetLevel(CurrentQualitySetting),CurrentQualitySetting.countFactor);
        }
        
        public void RemoveParticleCountGrading(ParticleCountGrading countGrading)
        {
            if(countGrading==null) return;
            _particleCountGradings.Remove(countGrading);
        }
        

        private int GetLevel(ParticleQualitySetting qualitySetting)
        {
            int level = 2;
            switch (qualitySetting.Quality)
            {
                case QualityEnum.Low:
                case QualityEnum.Medium:
                    level = 2;
                    break;
                case QualityEnum.High:
                case QualityEnum.VeryHigh:
                    level = 1;
                    break;
                case QualityEnum.Perfect:
                    level = 0;
                    break;
            }

            return level;
        }
        
        public override void ChangeQualitySetting(ParticleQualitySetting qualitySetting)
        {
            int level = GetLevel(qualitySetting);
            if (_particleGradings!=null)
            {
                foreach (var grading in _particleGradings)
                {
                    if (grading!=null)
                    {
                        grading.ChangeLevel(level);
                    }
                }
            }

            if (_particleCountGradings!=null)
            {
                foreach (var countGrading in _particleCountGradings)
                {
                    if (countGrading!=null)
                    {
                        countGrading.ChangeLevel(level,qualitySetting.countFactor);
                    }
                }
            }
            
        }

#if UNITY_EDITOR

        public void GetGradingParticleObject()
        {
            ParticleGradingList = _particleGradings.ToList();
            ParticleCountGradingList = _particleCountGradings.ToList();
        }
        
#endif
        
    }
}